package com.scqkxx.gamebox.receiver;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by zsx on 2018/8/1.
 */

public class IntentStartService extends Service {
    private static final String TAG = "IntentStartService";


    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        try {
            Log.e(TAG, intent.getStringExtra("payload") + "");
            String payload = intent.getStringExtra("payload") + "";

            JSONObject jsonobject = null;//str表示你取到的内容
            try {
                jsonobject = new JSONObject(payload);
                String type = jsonobject.optString("type");
                String content = jsonobject.optString("content");
                String title = jsonobject.optString("title");
                String url = jsonobject.optString("url");
                String jumpObjectId = jsonobject.optString("jumpObjectId");
                startNotification(this, type, title, url, jumpObjectId);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        } catch (Exception e) {

        }
        return super.onStartCommand(intent, flags, startId);
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }


    private void startNotification(final Context context, String type, final String title, String url, String jumpObjectId) {
        final Intent resultIntent = new Intent();
        resultIntent.putExtra("title", title);
        if (url != null && url.length() > 0) {
            resultIntent.putExtra("url", url);
        }

    }

    private void startIntent(Intent intent) {
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }
}
